package ar.com.tesis.mobile.activities;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.github.mrengineer13.snackbar.SnackBar;
import com.nineoldandroids.animation.Animator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.pnikosis.materialishprogress.ProgressWheel;

import ar.com.tesis.mobile.R;
import ar.com.tesis.mobile.Utils.FuentesUtil;
import ar.com.tesis.mobile.model.Usuario;

public abstract class ActividadBasica extends ActionBarActivity {

    public void setearFuentesLayoutDesconexion() {

        FuentesUtil.aplicarFuente(this, FuentesUtil.Fuente.TREBUCHET_MS_BOLD, (TextView) findViewById(R.id.desconexion_mensaje_corto));
        FuentesUtil.aplicarFuente(this, FuentesUtil.Fuente.TREBUCHET_MS, (TextView) findViewById(R.id.desconexion_mensaje_largo));
        FuentesUtil.aplicarFuente(this, FuentesUtil.Fuente.LATO_BLACK, (TextView) findViewById(R.id.reintentar));
    }

    public boolean estaConectado() {

        ConnectivityManager cm = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }

        return false;
    }

    public void mostrarBarraProgreso(final boolean mostrar, final ProgressWheel barraDeProgreso,
                                     TextView mensaje) {

        barraDeProgreso.setVisibility(mostrar ? View.VISIBLE : View.GONE);
        if (mensaje != null) {
            mensaje.setVisibility(mostrar ? View.VISIBLE : View.GONE);
        }
    }

    public void mostrarComoVisible(View view) {

        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public void ocultarComoGone(View view) {

        if (view != null) {
            view.setVisibility(View.GONE);
        }
    }

    public void ocultarComoInvisible(View view) {

        if (view != null) {
            view.setVisibility(View.INVISIBLE);
        }
    }

    public void mostrarPantallaDesconexion(boolean mostrar) {

        findViewById(R.id.layout_desconexion).setVisibility(mostrar ? View.VISIBLE : View.GONE);
    }

    public void setBotonReintentar() {

        findViewById(R.id.reintentar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                reintentarCallback();
            }
        });
    }

    public abstract void reintentarCallback();

    public void mostrarImagen(String url, ImageView imageView) {

        ImageLoader.getInstance().displayImage(url, imageView);
    }

    public Usuario obtenerUsuarioActual() {

        return Usuario.findById(Usuario.class, 1l);
    }

    public void hacerVisibleLentamente(final View vista, Techniques animacion, long duracion) {

        YoYo.with(animacion).duration(duracion)
                .withListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        mostrarComoVisible(vista);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                })
                .interpolate(new AccelerateDecelerateInterpolator())
                .playOn(vista);
    }

    public void hacerInvisibleLentamente(final View vista, long duracion) {

        YoYo.with(Techniques.FadeOut).duration(duracion)
                .interpolate(new AccelerateDecelerateInterpolator())
                .playOn(vista);
    }

    public void mostrarMensaje(String mensaje) {

        new SnackBar.Builder(this)
                .withOnClickListener(listenerSnack)
                .withMessage(mensaje)
                .withActionMessageId(android.R.string.ok)
                .withStyle(SnackBar.Style.CONFIRM)
                .withDuration(SnackBar.MED_SNACK)
                .show();
    }

    private SnackBar.OnMessageClickListener listenerSnack = new SnackBar.OnMessageClickListener() {
        @Override
        public void onMessageClick(Parcelable parcelable) {
        }
    };

}
